import TabMenu from './data';

var app = getApp();


Component({
  data: {
    active: 0,
    list: TabMenu,
  },

  methods: {
    onChange(event) {
      this.setData({
        active: event.detail.value
      });
      app.globalData.router = event.detail.value
      wx.switchTab({
        url: this.data.list[event.detail.value].url.startsWith('/') ?
          this.data.list[event.detail.value].url : `/${this.data.list[event.detail.value].url}`,
      });
    },

    init() {
      const userInfo = wx.getStorageSync('userInfo')
      const userType = userInfo.userType
      console.log('usertype', userType)
      if ([1, 2].includes(userType) && userInfo.status) {
        this.setData({
          list: [
            TabMenu[0],
            TabMenu[1],
            {
              icon: 'add',
              text: '发布',
              url: 'pages/publish/publish',
            },
            TabMenu[2],
          ]
        })
      } else {
        this.setData({
          list: TabMenu
        })
      }
      const page = getCurrentPages().pop();
      const route = page ? page.route.split('?')[0] : '';
      const active = this.data.list.findIndex(
        (item) => (item.url.startsWith('/') ? item.url.substr(1) : item.url) === `${route}`,
      );
      this.setData({
        active
      });
    },
  },
});